﻿namespace Shortcuts.ViewModels
{
    using Microsoft.Phone.Tasks;
    using Shortcuts.Commands;
    using Shortcuts.Common;
    using Shortcuts.Globalization;
    using System;
    using System.Windows;
    using System.Windows.Input;

    public class HomePageContext : System.ComponentModel.INotifyPropertyChanged
    {
        public HomePageContext()
        {
            WiFi = Localized.Get(Settings.WiFi.Named());
            Airplane = Localized.Get(Settings.Airplane.Named());
            Cellular = Localized.Get(Settings.Cellular.Named());
            Bluetooth = Localized.Get(Settings.Bluetooth.Named());
            Title = Localized.Get("SHORTCUTS");
            Pin = Localized.Get("pin to start");
            Open = Localized.Get("open");

            foreach (var tile in TileHelper.Tiles)
            {
                switch (tile.Settings)
                {
                    case Settings.Airplane:
                        AirplanePinCommand.IsEnabled = false;
                        break;
                    case Settings.Cellular:
                        CellularPinCommand.IsEnabled = false;
                        break;
                    case Settings.Bluetooth:
                        BluetoothPinCommand.IsEnabled = false;
                        break;
                    case Settings.WiFi:
                        WiFiPinCommand.IsEnabled = false;
                        break;
                }
            }
        }

        Command tileCommand;
        public ICommand TileCommand
        {
            get
            {
                if (tileCommand == null)
                {
                    tileCommand = new Command(parar =>
                    {
                        Settings settings = (Settings)Enum.Parse(typeof(Settings), parar.ToString(), true);
                        ConnectionSettingsTask task = new ConnectionSettingsTask();
                        task.ConnectionSettingsType = settings.ToConnection();
                        task.Show();
                    });
                }

                return tileCommand;
            }
        }

        Command wifiPinCommand;
        public Command WiFiPinCommand
        {
            get
            {
                if (wifiPinCommand == null)
                {
                    wifiPinCommand = new Command(parar =>
                    {
                        WiFiPinCommand.IsEnabled = false;
                        new Tile(Settings.WiFi).Pin();
                    });
                }

                return wifiPinCommand;
            }
        }

        Command airplanePinCommand;
        public Command AirplanePinCommand
        {
            get
            {
                if (airplanePinCommand == null)
                {
                    airplanePinCommand = new Command(parar =>
                    {
                        AirplanePinCommand.IsEnabled = false;
                        new Tile(Settings.Airplane).Pin();
                    });
                }

                return airplanePinCommand;
            }
        }

        Command bluetoothPinCommand;
        public Command BluetoothPinCommand
        {
            get
            {
                if (bluetoothPinCommand == null)
                {
                    bluetoothPinCommand = new Command(parar =>
                    {
                        BluetoothPinCommand.IsEnabled = false;
                        new Tile(Settings.Bluetooth).Pin();
                    });
                }

                return bluetoothPinCommand;
            }
        }

        Command cellularPinCommand;
        public Command CellularPinCommand
        {
            get
            {
                if (cellularPinCommand == null)
                {
                    cellularPinCommand = new Command(parar =>
                    {
                        CellularPinCommand.IsEnabled = false;
                        new Tile(Settings.Cellular).Pin();
                    });
                }

                return cellularPinCommand;
            }
        }

        #region properties

        string wifi;
        public string WiFi
        {
            get
            {
                return wifi;
            }
            set
            {
                if (wifi != value)
                {
                    wifi = value;
                    NotifyPropertyChanged("WiFi");
                }
            }
        }

        string airplane;
        public string Airplane
        {
            get
            {
                return airplane;
            }
            set
            {
                if (airplane != value)
                {
                    airplane = value;
                    NotifyPropertyChanged("Airplane");
                }
            }
        }

        string bluetooth;
        public string Bluetooth
        {
            get
            {
                return bluetooth;
            }
            set
            {
                if (bluetooth != value)
                {
                    bluetooth = value;
                    NotifyPropertyChanged("Bluetooth");
                }
            }
        }

        string cellular;
        public string Cellular
        {
            get
            {
                return cellular;
            }
            set
            {
                if (cellular != value)
                {
                    cellular = value;
                    NotifyPropertyChanged("Cellular");
                }
            }
        }

        string title;
        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                if (title != value)
                {
                    title = value;
                    NotifyPropertyChanged("Title");
                }
            }
        }

        string pin;
        public string Pin
        {
            get
            {
                return pin;
            }
            set
            {
                if (pin != value)
                {
                    pin = value;
                    NotifyPropertyChanged("Pin");
                }
            }
        }

        string open;
        public string Open
        {
            get
            {
                return open;
            }
            set
            {
                if (open != value)
                {
                    open = value;
                    NotifyPropertyChanged("Open");
                }
            }
        }

        #endregion

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        void NotifyPropertyChanged(string property)
        {
            System.ComponentModel.PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new System.ComponentModel.PropertyChangedEventArgs(property));
            }
        }
    }
}
